package models

import (
	"gorm.io/gorm"
)

// 用户表
type User struct {
	gorm.Model
	Username string `gorm:"type:varchar(50)"`
	Password string `gorm:"type:varchar(255)"`
}

// 用户-角色
type UserRole struct {
	gorm.Model
	Uid int `gorm:"type:int(11)"`
	Rid int `gorm:"type:int(11)"`
}

// 角色表
type Role struct {
	gorm.Model
	RoleName string `gorm:"type:varchar(50)"`
}

// 角色-权限
type RoleNode struct {
	gorm.Model
	Rid int `gorm:"type:int(11)"`
	Nid int `gorm:"type:int(11)"`
}

// 权限表
type Node struct {
	gorm.Model
	NodeName string `gorm:"type:varchar(50)"`
}

// 物流企业表
type Comp struct {
	gorm.Model
	CompName string `gorm:"type:varchar(50)"`
}

// 快递类型表
type Exp struct {
	gorm.Model
	ExpName string `gorm:"type:varchar(50)"`
}

// 快递订单表
type Orders struct {
	gorm.Model
	OrdersNum string `gorm:"type:varchar(50)"`
	Cid       int    `gorm:"type:int(11)"`
	Eid       int    `gorm:"type:int(11)"`
}

// 订单联查字段
type OrdersListData struct {
	OrdersNum string `json:"orders_num"`
	Cid       int    `json:"cid"`
	CompName  string `json:"comp_name"`
	Eid       int    `json:"eid"`
	ExpName   string `json:"exp_name"`
}

// -------------------------------------------------------------------------------

func (*User) LoginUser(username string, user *User) error {
	return DB.Where("username=?", username).First(&user).Error
}

func (*RoleNode) RoleNodeAdd(data *RoleNode) (int, error) {
	tx := DB.Begin()

	res := DB.Debug().Create(&data).Error
	if res != nil {
		tx.Rollback()
		return 0, res
	}
	tx.Commit()
	return int(data.ID), nil
}

func (*Comp) Compadd(data *Comp) (int, error) {
	tx := DB.Begin()
	res := tx.Create(&data)
	if res.Error != nil {
		tx.Rollback()
		return 0, res.Error
	}
	tx.Commit()
	return int(data.ID), nil
}

func (*Orders) Ordersadd(data *Orders) (int, error) {
	tx := DB.Begin()
	res := DB.Debug().Create(&data)
	if res.Error != nil {
		tx.Rollback()
		return 0, res.Error
	}
	tx.Commit()
	return int(data.ID), nil
}
